
export const findStore = (obj) => {
  if (obj.parent && obj.parent.parent) {
    return findStore(obj.parent);
  }
  return obj.parent;
};

export const findSimulator = (mod) => {
  const store = findStore(mod);
  return store.simulator;
};

export const findProject = (mod) => {
  // console.log(mod);
  const store = findStore(mod);
  return store.project;
};

/**
 * 查找 mod
 * @param obj
 */
export const findModule = (obj) => {
  if (obj.parent && !obj.originType) {
    return findModule(obj.parent);
  }
  return obj;
};


export const findDeepestCurrent = (obj) => {
  if (obj.current) {
    if (obj.current.current) {
      return findDeepestCurrent(obj.current);
    } else {
      return obj.current;
    }
  } else {
    return obj;
  }
};

export const findParentPages = (obj) => {
  if (obj.originType === 'Pages') {
    return obj;
  }
  if (obj.parent && obj.parent.originType !== 'Pages') {
    return findParentPages(obj.parent);
  } else {
    return obj.parent;
  }
};
export const findRootPages = (obj) => {
  if (obj.originType === 'Pages' && obj.isRoot) {
    return obj;
  }
  if (obj.parent && obj.parent.originType !== 'Pages' && !obj.parent.isRoot) {
    return findRootPages(obj.parent);
  } else {
    return obj.parent;
  }
};


export const loadModulesModel = (type) => {
  return new Promise((resolve, reject) => {
    if (type) {
      if (CFG.cache.modules[type]) {
        const Model = CFG.cache.modules[type];
        // console.log('from cache');
        resolve(Model);
      } else {
        try {
          import(`modules/${type}/model`).then((resp) => {
            // console.log('resp:', resp);
            class Model extends resp.default {}
            CFG.cache.modules[type] = Model;
            // console.log(Model);
            resolve(Model);
          });
        } catch (e) {
          reject();
          console.warn(`${type} Model addModule error`, e);
        }
      }
    } else {
      reject();
      console.warn(`loadModel fail.  item.type = ${type}`);
    }
  });
};
